﻿package com.findMoneyGame.models {
	import flash.geom.Rectangle;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.EventDispatcher;

	import com.findMoneyGame.server.ServerFacadeFactory;
	import com.findMoneyGame.events.GameLevelEvent;
	import com.findMoneyGame.models.GameLevelData;

	public class GameLevel extends EventDispatcher {
		private var serverFacade = ServerFacadeFactory.NewInst;
		private var _targetRect : Rectangle = new Rectangle(20, 20, 100, 100);
		private var _image : BitmapData;
		private var _data : GameLevelData;
		
		public function get image () : BitmapData { return _image; }
		public function set image (value : BitmapData) { _image = value; }
		public function get data () : GameLevelData { return _data; }
		public function set data (value : GameLevelData) { _data = value; }
		public function get bytesTotal () : int { return serverFacade.imageBytesTotal; }
		public function get bytesLoaded () : int { return serverFacade.imageBytesLoaded; }
		public function get loadingPercent () { return bytesLoaded / bytesTotal; }
		public function get targetRectangle () { return _targetRect; }


		public function GameLevel (level : int) {
			_data = new GameLevelData();
			_data.level = level;
		}
		public function beginLoadImage (complete : Function = null, error : Function = null) {
			var reqComplete = function (bmp : Bitmap) {
				image = bmp.bitmapData;
				var e = new Event(GameLevelEvent.IMAGE_LOADED);
				dispatchEvent(e);
				if (complete != null) {
					complete(e);
				}
			};
			var reqError = function (e : Event) {
				var event = new Event(GameLevelEvent.IMAGE_LOADING_ERROR);
				dispatchEvent(event);
				if (error != null) {
					error(event);
				}
			};
			ServerFacadeFactory.NewInst.beginLoadImage(_data.imageUrl, reqComplete, reqError);
		}
		public function beginFetchData (complete : Function = null, error : Function = null) {
			var reqComplete = function (result : *) {
				data.reward = result.reward;
				data.bonusTimeReward = result.bonusTimeReward;
				data.passLevelPrice = result.passLevelPrice;
				data.hintPrice = result.hintPrice;
				data.loupePrice = result.loupePrice;
				data.loupeTime = result.loupeTime;
				data.hintTime = result.hintTime;
				data.failingPrice = result.failingPrice;
				data.targetRectX = result.targetRectX;
				data.targetRectY = result.targetRectY;
				data.targetRectWidth = result.targetRectWidth;
				data.targetRectHeight = result.targetRectHeight;
				data.imageUrl = result.imageUrl;
				var event = new Event(GameLevelEvent.FETCH_DATA_REQUEST_COMPLETE);
				dispatchEvent(event);
				if (complete != null) {
					complete(event);
				}
			};
			var reqError = function (e : Event) {
				var event = new Event(GameLevelEvent.FETCH_DATA_REQUEST_ERROR);
				dispatchEvent(event);
				if (error != null) {
					error(event);
				}
			};
			ServerFacadeFactory.NewInst.beginFetchGameLevel(data.level, reqComplete, reqError);
		}
	}
}